var ctx;
var uname = "";
var type = 0;
var attrId ;
var selectRu = [];

function setCtx(ctx){
	this.ctx = ctx;
}

var listSize;
var len = $("#listSize").val();
var isAddValue = false;

//新增或编辑页面点击添加按钮追加一条数据
function addValue(){
/*	if(removeDict){
		len = --len
	}*/
	len = ++len;
	var index = len-1;
	$("#aedict").append('<div id="avalueId' +index + '" class="item">'+
			'<label class="col-sm-3 editlable">选项'+ len +'文本值：</label>'+
			'<div class="col-sm-2 editdiv" style="margin-left:15px;margin-top:3px;">'+
				'<input id="name' +index + '" type="text" maxlength="50" value="" class="form-control1" placeholder="属性值" required>'+
			'</div>'+
			'<div class="col-sm-2" style="margin-left:5px;">'+
				'<a class="search" onclick="deleteValue('+index+');" style="float:left;margin-left:31px;margin-top:3px;"><img width="80%;" src="/manage/images/table/delete.png"></a>'+
			'</div></div>');
	isAddValue = true;
	
	initPlaceHolder();
}

function deleteValue(valueId){
	$("#avalueId" + valueId).remove();
}

var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
function saveOrUpdateAttrValue(){
	if(isAddValue){
		listSize = len;
	}else{
		listSize = $("#listSize").val();
	}
	var list = [];
	for(var i=0;i < listSize;i++){
		var attrValues = {};
		attrValues.name = $("#name" + i).val();
		/* list.push("{'dictId':"+$("#dictId" + i).val()+",'label':'"+$("#label" + i).val()+"','value':"+$("#value" + i).val()+",'sort':"+$("#sort" + i).val()+"}") */
		//解决ie9以下placeholder与value重复的问题
		if(attrValues.name == $("#name" + i).attr("placeholder")){
			attrValues.name = null;
		}
		if(attrValues.name==null){
			continue;
		}
		list.push(attrValues);
	}
	var attrId = $("#attrId").val();
	

	var index1 = layer.load(1);
		$.post(ctx+"/goodsCategory/saveOrUpdateAttrValue",{
			values:JSON.stringify(list),
			attrId:attrId
		},function(data){
			data = JSON.parse(data);
			if(data.success){
				layer.msg("操作成功",{
					icon: 1,
					time: 2000 //2秒关闭（如果不配置，默认是3秒）
				},function(){
					parent.loadContent("/goodsCategory/categoryList");
					parent.layer.close(index);
				});
			} else {
				layer.close(index1);
				layer.msg(data.msg,{
					icon: 2,
					time: 2000 //2秒关闭（如果不配置，默认是3秒）
				});
			}
		});
}
